package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import com.abc.robot01.activity.MYBLACK_Percent5
import com.abc.robot01.activity.MyButton01
import com.abc.robot01.activity.MyFont_Size
import com.abc.robot01.activity.TwoStateButton
import com.abc.robot01.activity.mysp
import com.abc.robot01.activity.px
import com.abc.robot01.vivemodel.FangAnKuViewModel

class PopupDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val  names= listOf("木质","陶瓷","金属","石材")
        val  names2= listOf("简约","舒适","豪华")
        setContent {
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
            ) {
                PopupExample()
//                BasicPopupDemo(names,names2)
//                PopupDemo2()
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
            }
        }
    }
}

@Composable
fun PopupExample() {
    var firstPopupOpen by remember { mutableStateOf(false) }
    var secondPopupOpen  =  remember { mutableStateOf(false) }

    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        // Main Button to trigger the first Popup
        Button(onClick = { firstPopupOpen = !firstPopupOpen }) {
            Text("Open First Popup")
        }

        // First Popup
        if (firstPopupOpen) {
            Popup(
                onDismissRequest = { firstPopupOpen = false },
                alignment = Alignment.TopStart
            ) {
                Box(
                    modifier = Modifier.width(1000.dp)
                        .padding(16.dp)
                        .background(Color.Cyan),
                    contentAlignment = Alignment.Center
                ) {
                    Column(horizontalAlignment = Alignment.CenterHorizontally) {
                        Text("This is the first Popup")
                        Spacer(modifier = Modifier.height(16.dp))
                        Button(onClick = { secondPopupOpen.value = !secondPopupOpen.value }) {
                            Text("Open Second Popup")
                        }
                    }

                    // Nested Popup (Second Popup)
                    if (secondPopupOpen.value) {
                        SecondPopup2(secondPopupOpen)
                    }
                }
            }
        }
    }
}




fun clicked(tag:String){



}

@Composable
fun BasicPopupDemo(list: List<String>, list2: List<String>) {
    val showPopup = remember { mutableStateOf(false) }
    val selected1 = remember { mutableStateOf("全部") }
    val selected2 = remember { mutableStateOf("全部") }
    val names = remember { list }
    val names2 = remember { list2 }

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0x00667788))
    ) {
        Button(onClick = { showPopup.value = true }) {
            Text(text = "Show Popup")
        }
        if (showPopup.value) {
            Popup(
                alignment= Alignment.TopStart,
                offset =  IntOffset(1*30, 1*160),
                onDismissRequest = { showPopup.value = false }) {
                Card(modifier = Modifier.size( (1026).dp/3.5f,(232).dp/3.5f)) {
                    Column(modifier = Modifier
                        .fillMaxWidth()
//                        .background(Color.Red)
                        .offset(y = 10.dp, x = 10.dp))
                             {
                        Row(verticalAlignment=Alignment.CenterVertically) {
                            Text(text = "适合喜欢", fontSize = MyFont_Size)
                            names2.forEach {
                                Spacer(modifier = Modifier.width(6.dp))
                                TwoStateButton(selected1,"$it" , onClick = ::clicked)
                            }
                        }
                        Spacer(modifier = Modifier.height(4.dp))
                        Row(verticalAlignment=Alignment.CenterVertically) {
                            Spacer(modifier = Modifier.width(14.dp))
                            Text(text = "材料", fontSize = MyFont_Size)
                            names.forEach {
                                Spacer(modifier = Modifier.width(6.dp))
                                TwoStateButton(selected2,"$it",  onClick = ::clicked)
                            }
                        }

                                 Row(verticalAlignment=Alignment.CenterVertically,
                                     modifier = Modifier
                                         .fillMaxWidth().offset(y=10.dp)
                                         .wrapContentWidth(Alignment.CenterHorizontally)
                                     ) {
                                     MyButton01(text = "重置",containerColor= MYBLACK_Percent5,textColor=Color.Black){
                                         selected1.value=""
                                         selected2.value=""
                                     }
                                     Spacer(modifier = Modifier.width(20.dp))
                                     MyButton01(text = "确认",containerColor=Color.Blue,textColor=Color.White){
                                         showPopup.value = false
                                     }
                                 }
                             }
                }
            }
        }
    }
}

@Composable
fun BasicPopupDemo02() {
    val showPopup = remember { mutableStateOf(false) }

    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier
            .height(300.dp)
            .fillMaxWidth()
            .background(Color(0xff667788))
    ) {
        Button(onClick = { showPopup.value = true }) {
            Text(text = "Show Popup")
        }

        if (showPopup.value) {
            Popup(
                alignment= Alignment.Center,
                onDismissRequest = { showPopup.value = false }) {
                Box(
                    modifier = Modifier
                        .padding(20.dp)
                        .padding(50.dp)
                        .clickable { showPopup.value = false }
                ) {
                    Text(text = "This is a basic popup")
                }
            }
        }
    }
}

@Composable
fun SecondPopup(secondPopupOpen: MutableState<Boolean>,
                width: Dp = 374.px,
                offsetY: Dp = 0.px,
                offsetX: Dp = 0.px,

) {
    Popup(
        onDismissRequest = { secondPopupOpen.value = false },
        alignment = Alignment.TopStart
    ) {
        Box(
            modifier = Modifier.width(width)
                .offset(y = offsetY,x = offsetX)
                .padding(0.dp)
                .background(Color.Red),
            contentAlignment = Alignment.Center
        ) {
            Column {
                Text("This is the second Popup with a list")
                Spacer(modifier = Modifier.height(8.dp))

                // List inside the second Popup
                Column(modifier = Modifier.fillMaxWidth()) {
                    listOf("Item 1", "Item 2", "Item 3", "Item 4").forEach {
                        Text(it, modifier = Modifier.padding(8.dp))
                    }
                }
            }
        }
    }
}

@Composable
fun SecondPopup2(secondPopupOpen: MutableState<Boolean>,
//                 selected: MutableState<String>,
//                 viewModel: FangAnKuViewModel,
                width: Dp = 374.px,
                offsetY: Dp = 0.px,
                offsetX: Dp = 0.px,

                ) {
    val items: List<Pair<String, String>> = listOf(
        "Item 1" to "Description 1",
        "Item 2" to "Description 2",
        "Item 3" to "Description 3",
        "Item 4" to "Description 4",
        "Item 5" to "Description 5"
    )

//    val items: List<Pair<String,String>> by viewModel.designerList.collectAsState()
//    val items: List<Pair<String,String>> by viewModel.designerList.collectAsState()
    Popup(
        onDismissRequest = { secondPopupOpen.value = false },
        alignment = Alignment.TopStart
    ) {
        Box(
            modifier = Modifier.width(width).height(1000.dp)
                .offset(y = offsetY,x = offsetX)
                .padding(0.dp)
                .background(Color.Red),
            contentAlignment = Alignment.Center
        ) {
            LazyColumn {
                itemsIndexed(items, key = { index, item -> item.first }) {  index, it ->
                    val isLastItem = index == items.lastIndex
                    Box(
                        modifier = Modifier
//                                    .background(
//                                        color = Color.Transparent,
//                                        shape = RoundedCornerShape(5.px))
                            .fillMaxWidth()
                            .height(80.px), // 修改为 dp 或者其他单位
                        contentAlignment = Alignment.CenterStart // 垂直和水平居中
                    ) {
                        BasicText(
                            modifier = Modifier
                                .clickable {
//                                    expanded.value = false
//                                    selected.value = it.second
//                                    viewModel.selectedDesigner.value = it.first
                                }.offset(x=30.px),
                            text = it.second,
                            style = TextStyle(
                                fontWeight = FontWeight.Normal,
                                textAlign = TextAlign.Start,
                                fontSize = 26.mysp,
                                color = Color.Black,
                            ),
                        )
                    }
                }
            }
        }
    }
}









